package demo1;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-26
 * Time: 17:15
 */
public class Test3 {
    public static void main(String[] args) {
        //不使用lambda表达式
        HashMap<Integer, String> map1 = new HashMap<>();
        map1.put(1, "hello");
        map1.put(2, "word");
        map1.put(3, "bit");
        map1.put(4, "lambda");
        map1.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.print(integer + "=" + s + " ");
            }
        });
        System.out.println();
        System.out.println("=============");
        //使用lambda表达式
        HashMap<Integer, String> map2 = new HashMap<>();
        map2.put(1, "hello");
        map2.put(2, "word");
        map2.put(3, "bit");
        map2.put(4, "lambda");
        map2.forEach((Integer a, String b) -> {System.out.print(a + "=" + b + " ");});
    }
    public static void main2(String[] args) {
        //不使用lambda表达式
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("hello");
        list1.add("word");
        list1.add("bit");
        list1.add("lambda");
        list1.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        System.out.println(list1);
        System.out.println("===================");
        //使用lambda表达式
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("word");
        list2.add("bit");
        list2.add("lambda");
        list2.sort((String o1, String o2) -> {return o1.length() - o2.length();});
        System.out.println(list2);
    }
    public static void main1(String[] args) {
        //不使用lambda表达式
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("hello");
        list1.add("word");
        list1.add("bit");
        list1.add("lambda");
        list1.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s + " ");
            }
        });
        System.out.println();
        System.out.println("=============");
        //使用lambda表达式
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("word");
        list2.add("bit");
        list2.add("lambda");

        list2.forEach((String x) -> {System.out.print(x + " ");});
    }
}
